#ifndef __INTERVAL_H
#define __INTERVAL_H

#ifdef __cplusplus
extern "C"
{
#endif

#ifdef USE_HAL_DRIVER
#include "main.h"
#define HAL_MS_COUNT uwTick
#define SYSTICK_COUNT SysTick->VAL
#define SYSTEM_CLOCK_HZ SystemCoreClock
#define HAL_TIME_BASE_HZ 1000U
#else
#error Only used in STM32 projects.
#endif

#define NODE_ID_DEFAULT -1
#define TIME_INTERVAL_CAL_TO_US 1000000U
#define TIME_INTERVAL_CAL_TO_MS 1000U

    /*双向链表结构体类型*/
    typedef struct time_interval
    {
        /* data */
        int32_t node_id;
        uint32_t hal_ms;
        uint32_t ticks1;
        uint32_t ticks2;
        uint32_t ticks;
        struct time_interval *previous;
        struct time_interval *next;
    } time_interval_t;

    typedef enum
    {
        NODE_MEM_ERROR = -4,
        NODE_ID_ERROR = -3,
        NODE_ID_NOT_EXIST = -2,
        NODE_ID_ALREADY_EXIST = -1,
        NODE_OK = 0
    } node_error_no_t;

    enum time_interval_unit
    {
        TIME_US = 1,
        TIME_MS
    };

    node_error_no_t time_interval_init(time_interval_t **node);
    node_error_no_t time_interval_begin(int32_t node_id, time_interval_t *node);
    node_error_no_t time_interval_end(int32_t node_id, time_interval_t *node);
    node_error_no_t time_interval_cal(int32_t node_id, time_interval_t **node, uint32_t *time_interval, enum time_interval_unit unit);

#ifdef __cplusplus
}
#endif

#endif
